using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class app_frame_help : System.Web.UI.Page
{
    string strPagesetRowID = "ee9ae9b7cef177a2"; // ps_help
    DataSet dseInputInfo = new DataSet();
    DataSet dsePageInfo = new DataSet();
    HtmlForm frmObj;
    string strErrMessage = "";
    string refererURL = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Request.UrlReferrer!=null)
        //    if (Request.UrlReferrer.ToString().IndexOf("DisplayGrid") > 0)
        //        refererURL = Request.UrlReferrer.ToString();
        security hdds = new security();
        if (Session["userid"] == null) Response.Redirect(hdds.SessionExpiryURL);
        if (Session["userid"].ToString() == "") Response.Redirect(hdds.SessionExpiryURL);

        if (Request.QueryString["PK"] != null && Request.QueryString["pgs_id"] != null)
        {
            if (!hdds.checkRecordAccess(Request.QueryString["PK"].ToString(), Request.QueryString["pgs_id"].ToString()))
            {
                lbl_message.Text = wrapItAsMessage("<img src=\"Images/info.gif\" alt=\"\"></img>&nbsp;You do not have sufficient access permission.", true);
                tblcontrols.Visible = false;
                return;
            }
        }

        setRequestURL();
        loadDynamicControls();
        if (this.Master.FindControl("Form1") != null)
        {
            frmObj = (HtmlForm)this.Master.FindControl("Form1");
        }
        PageInfoLoad PIL = new PageInfoLoad();
        dsePageInfo = PIL.GetPageSetPageCtrlInfo(strPagesetRowID);
        if (!Page.IsPostBack)
        {
            //loadOwneridDropDown();
            p_help_owner_id.Value = Session["user_afrowid"].ToString();
            txt_p_help_owner_id.Text = Session["userid"].ToString();
        }
        CheckForChangeOwner();
        CheckEditOperation();
        //p_help_help_desc.Visible=false;
        if (IsPostBack) return;
        loadCategory();
        if (btnUpdate.Visible)
        {
            lblHeader.Text = "Help Kit - Edit";
            loadMasterValues();
        }
        if (Request.QueryString["CPY"] != null)
        {
            if (Request.QueryString["CPY"] == "0")
            {
                lblHeader.Text = "Help Kit - Copy";
                loadMasterValues();
                //showParamterContent(true, Request.QueryString["PK"].ToString());
                //p_function_register_id.Text = getSeqNumber();
                dd_p_help_owner_id.SelectedValue = Session["user_afrowid"].ToString();
                txt_p_help_owner_id.Text = Session["userid"].ToString();
                p_help_owner_id.Value = Session["user_afrowid"].ToString();
            }
        }

        isViewMode();
        getpageicons();//added by srivatsan
    }
    private void isViewMode()
    {
        if (HttpContext.Current.Request.QueryString["VMODE"] != null)
            if (HttpContext.Current.Request.QueryString["VMODE"] == "1")
            {
                lblHeader.Text = lblHeader.Text = "Help Kit - View";
                DynamicDisplayI ddi = new DynamicDisplayI();
                HtmlTable ht = (HtmlTable)frmObj.FindControl("ContentPlaceHolder3").FindControl("TABLE1");
                ddi.disableControls(ht);
                //fred.Visible = false; //srivatsan
                fred.Disabled = false;
                p_help_help_desc.Enabled = true;
                ddi = null;
            }
    }    
    private void loadMasterValues()
    {
        try
        {
            string sPageKey = "";
            if (Request.QueryString["PK"] != null)
                sPageKey = Request.QueryString["PK"].ToString();
            if (sPageKey == "") return;
            HDDatabase hd = new HDDatabase();
            DataSet ds = new DataSet();
            //ds = hd.GetDataset("select * from sy_help where help_id='" + sPageKey + "'");
            ds = hd.GetDataset("select * from sy_help where af_row_id='" + sPageKey + "'");
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    p_help_af_row_id.Value = sPageKey;
                    p_help_help_id.Text = ds.Tables[0].Rows[0]["help_id"].ToString();
                    p_help_help_desc.Text = ds.Tables[0].Rows[0]["help_desc"].ToString();
                    //p_message_msg_language.Text = ds.Tables[0].Rows[0]["help_language"].ToString();
                    p_help_help_title.Text = ds.Tables[0].Rows[0]["help_title"].ToString();
                    p_help_help_seq.Text = ds.Tables[0].Rows[0]["help_seq"].ToString();
                    p_help_help_category.SelectedValue= ds.Tables[0].Rows[0]["help_category"].ToString();
                    //dd_p_help_owner_id.SelectedValue = ds.Tables[0].Rows[0]["owner_id"].ToString();
                    p_help_owner_id.Value = ds.Tables[0].Rows[0]["owner_id"].ToString();
                    LoadEditPage LEP = new LoadEditPage();
                    txt_p_help_owner_id.Text = LEP.GetUserIDFromAfrowid(ds.Tables[0].Rows[0]["owner_id"].ToString());
                    LEP = null;
                    p_help_help_id.Enabled = false;
                }
            }

            ds = null;
            hd = null;
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in messages.aspx:loadMasterValues: " + e.Message);
        }
    }
    private void loadDynamicControls()
    {
        //if (IsPostBack) return;
        HiddenField hf = new HiddenField();
        hf.ID = "hf_ref_url";
        //hf.EnableViewState = true;
        if (refererURL.Length > 0) hf.Value = refererURL;
        lbl_message.Controls.Add(hf);
    }
    private void setRequestURL()//implemented by sanal on 13 Dec 2007
    {
        if (!IsPostBack)
            //hfRequestURL.Value = Request.UrlReferrer.ToString();
            hfRequestURL.Value = "DisplayGrid.aspx?pgs_id=" + Request.QueryString["pgs_id"].ToString() + "&mn=" + Request.QueryString["mn"].ToString();
    }
    private void loadCategory()
    {
        HDDatabase hd = new HDDatabase();
        DataSet ds = hd.GetDataset("select DISTINCT hc_name from sy_help_category");
        p_help_help_category.Items.Add(new ListItem("", ""));
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                    p_help_help_category.Items.Add(new ListItem(dr["hc_name"].ToString(), dr["hc_name"].ToString()));
            }
        }


    }
    private bool CheckEditOperation()
    {
        bool blnStatus = true;
        string strPageKey = "";
        bool IsEdit = false;
        if (Page.IsPostBack == false)
        {
            if (Request.QueryString["PK"] != null)
            {
                strPageKey = Request.QueryString["PK"].ToString();
                IsEdit = true;
            }
            if (IsEdit)
            {
                Master MA = new Master();
                blnStatus = MA.EditOperation(dsePageInfo, frmObj, strPageKey);
                if (blnStatus == false)
                {
                    strErrMessage = MA.ErrorMessage;
                    Response.Write("<font color='red'>Error : " + strErrMessage + "</font>");
                }
            }
        }
        if (Request.QueryString["CPY"] != null)
        {
            if (Request.QueryString["CPY"] == "0")
            {
                btnSave.Visible = true;
                btnUpdate.Visible = false;
            }
        }
        else if (Request.QueryString["PK"] == null)
        {
            btnSave.Visible = true;
            btnUpdate.Visible = false;
        }
        else
        {
            btnUpdate.Visible = true;
            btnSave.Visible = false;
        }
        return blnStatus;
    }



    private bool SaveData()
    {
        bool blnStatus = true;
        ScreenReader SR = new ScreenReader();
        dseInputInfo = SR.CaptureData(strPagesetRowID, frmObj, dsePageInfo,true);
        if (SR.errormessage != "")
        {
            strErrMessage = SR.errormessage;
            return false;
        }
        Master MA = new Master();
        blnStatus = MA.ProcessOperation(dsePageInfo, dseInputInfo, "INSERT");
        if (blnStatus == false)
        {
            strErrMessage = MA.ErrorMessage;
        }
        return blnStatus;
    }

    private bool UpdateData()
    {
        bool blnStatus = true;
        ScreenReader SR = new ScreenReader();
        dseInputInfo = SR.CaptureData(strPagesetRowID, frmObj, dsePageInfo,false);
        if (SR.errormessage != "")
        {
            strErrMessage = SR.errormessage;
            return false;
        }
        Master MA = new Master();
        blnStatus = MA.ProcessOperation(dsePageInfo, dseInputInfo, "UPDATE");
        if (blnStatus == false)
        {
            strErrMessage = MA.ErrorMessage;
        }
        return blnStatus;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        this.Theme = Session["Theme"].ToString();
        this.Title = "MaxBlox";
    }
    protected void btnSave_ServerClick(object sender, EventArgs e)
    {        
        //return;         
        string ret = p_help_help_desc.Text;        
        if (SaveData())
        {
            //Response.Write("Data Inserted Successfully");
            string refURL = getRefURL();
            if (refURL.Length > 0)
                Response.Redirect(refURL);
            else
                lbl_message.Text = "&nbsp;<img src='Images/info.gif' border='0'>&nbsp;Data Inserted Successfully.";//changed by prithi on 07-Aug-2008
        }
        else
        {
            lbl_message.Text = "<img src=\"Images/error.gif\" alt=\"Error\"></img>&nbsp;" + "Data Insert not successful. Error:" + ErrorMessage;
        }
    }
    protected void btnUpdate_ServerClick(object sender, EventArgs e)
    {
        if (UpdateData())
        {
            string refURL = getRefURL();
            if (refURL.Length > 0)
                Response.Redirect(refURL);
            else
                lbl_message.Text = "&nbsp;<img src='Images/info.gif' border='0'>&nbsp;Data Updated Successfully.";//changed by prithi on 07-Aug-2008
        }
        else
        {
            lbl_message.Text = "<img src=\"Images/error.gif\" alt=\"Error\"></img>&nbsp;" + "Data Update not successful. Error:" + ErrorMessage;
        }
    }

    protected void btnreset_ServerClick(object sender, EventArgs e)
    {
        //Response.Redirect(Request.RawUrl);
        try
        {
            p_help_help_desc.Text = "";
            //p_help_help_id = "";
            p_help_help_title.Text = "";            
            p_help_help_category.SelectedIndex = 0;
        }
        catch (Exception ee)
        {

        }
    }
    private string getRefURL()
    {
        string ret = "";
        try
        {
            //DynamicDisplayI di = new DynamicDisplayI();
            //HiddenField hf = (HiddenField)di.FindControl(this.Controls, "hf_ref_url");
            //ret = hf.Value;
            //di = null;
            //return ret;
            return hfRequestURL.Value;
        }
        catch (Exception e)
        {
            return "";
        }

    }
    
    public string ErrorMessage
    {
        get
        {
            return this.strErrMessage;
        }
    }

    public void getpageicons()// Added by srivatsan to get page icon
    {
        PageIcons icons = new PageIcons();
        HDDatabase Hdd = new HDDatabase();
        try
        {
            string strPagesetRowID = Request.QueryString["pgs_id"].ToString();

            lblHeader.Text = "&nbsp;" + icons.geticontextfrompageset(strPagesetRowID, 2) + "&nbsp;" + lblHeader.Text;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error :getpageicons() " + ex.Message);
            lblHeader.Text = "<img src=Images/MBIcons/small/default.png></img>&nbsp;" + lblHeader.Text;
        }
        finally
        {
            icons = null;
            Hdd = null;
        }

    }

    private void loadOwneridDropDown() // Added by srivatsan for owner id change
    {
        HDDatabase Hdd = new HDDatabase();
        BindControl BC = new BindControl();
        //DataSet ds = new DataSet();
        DataTable dt = null;
        try
        {
            string strBindRowID = Hdd.GetColumnValue("select mem_value3 from sy_acr_mem where acr_id = 'cee69885ce44589c' and mem_id = 'owner_id'");
            BC.Bind(strBindRowID, dd_p_help_owner_id, ref dt,null);
            if (Session["user_afrowid"].ToString() == "e51e0327224a2eb9")
            {
                dd_p_help_owner_id.Items.Add(new ListItem(Session["userid"].ToString(), Session["user_afrowid"].ToString()));
            }

            if (Request.QueryString["PK"] == null)
            {
                dd_p_help_owner_id.SelectedValue = Session["user_afrowid"].ToString();
            }
            if (Request.QueryString["PK"] != null)
            {
                if (Request.QueryString["CPY"] != null)
                {
                    dd_p_help_owner_id.SelectedValue = Session["user_afrowid"].ToString();
                }

            }

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : loadOwneridDropDown :" + ex.Message);
        }
        finally
        {
            Hdd = null;
            BC = null;
            dt = null;
        }


    }
    private void CheckForChangeOwner()  // Added by srivatsan to Check if user can change owner
    {
        try
        {
            security sec = new security();
            bool status = false;
            status = sec.getRightsForMRUObjects(HttpContext.Current.Request.QueryString["pgs_id"].ToString(), "change_owner");
            if (status == false)
            {
                txt_p_help_owner_id.Enabled = false;
                imgOwnerSearch.Visible = false;
            }
            sec = null;
        }
        catch
        {

        }
    }

    private string wrapItAsMessage(string msg, bool isError)
    {
        return "<div class=\"" + (isError == true ? "mbErrorMessage" : "mbMessage") + "\">" + msg + "</div>";
    }
}
